package net.java.nioserver;

import net.java.nioserver.operations.IoEventListener;
import org.slf4j.Logger;

import java.io.IOException;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;

/**
 * @author Leonid Shlyapnikov
 */
public class IoLogUtil {
    private IoLogUtil() {
    }

    public static void logAsynchronousCloseException(AsynchronousCloseException exception, Channel channel, Logger log) {
        final String msg = "Asynchronouse channel close: ";

        if (log.isInfoEnabled()) {
            log.info(msg + channel);
        } else if (log.isDebugEnabled()) {
            log.debug(msg + channel, exception);
        }
    }

    public static void logClosedChannelException(ClosedChannelException exception, Channel channel, Logger log) {
        if (log.isInfoEnabled()) {
            log.info("Closing channel: " + channel);
        } else if (log.isDebugEnabled()) {
            log.debug("Closing chanel: " + channel, exception);
        }
    }

    public static void logIOException(IOException e, IoEventListener listener, Channel channel, Logger log) {
        log.error("Cannot dispatch event to: " + listener + ", closing channel: " + channel, e);
    }
}
